package com.netease.xone.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;

import com.netease.pkgRelated.XoneConsts.BUNDLE_KEY;
import com.netease.pkgRelated.XoneConsts.WB_LOGIN_TYPE;
import com.netease.xone.R;
import com.netease.xone.fragment.FragmentWBLogin;


public class ActivityExternalLogin extends ActivityX1Base {
    private FragmentWBLogin mFragmentWBLogin;
    
    public static void launch(Context context, int type) {
        Intent i = new Intent(context, ActivityExternalLogin.class);
        i.putExtra(BUNDLE_KEY.WB_BIND_TYPE, type);
        context.startActivity(i);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout linear = new LinearLayout(this);
        linear.setId(R.id.activity_ExternalLogin_container_id);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linear.setLayoutParams(lp);
        setContentView(linear);
        Intent intent = getIntent();
        int type = 0;
        if(intent != null) {
            type = intent.getIntExtra(BUNDLE_KEY.WB_BIND_TYPE, 0);
        }
        if (findViewById(R.id.activity_ExternalLogin_container_id) != null
                && savedInstanceState == null) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            mFragmentWBLogin = FragmentWBLogin.newInstance(WB_LOGIN_TYPE.Login, type);
            ft.add(R.id.activity_ExternalLogin_container_id, mFragmentWBLogin);
            ft.commit();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (mFragmentWBLogin != null && mFragmentWBLogin.getSsoHandler() != null) {
            mFragmentWBLogin.getSsoHandler().authorizeCallBack(requestCode, resultCode, data);
        }
    }
}
